package com.example.MA.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.example.MA.common.dto.LoginDto;
import com.example.MA.common.lang.Result;
import com.example.MA.entity.UserInfo;
import com.example.MA.mapper.UserInfoMapper;
import com.example.MA.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        try{
            UserInfo user=userInfoService.getOne(new QueryWrapper<UserInfo>().eq("username",loginDto.getUsername()));
            Assert.notNull(user,"账号或密码有误");

            Map<String,String> map = new LinkedHashMap<>();
            map.put("id",user.getUserid().toString());
            map.put("username",user.getUsername());
            map.put("phone",user.getPhone());

            return Result.success(map);
        }catch (Exception e){
            return Result.fail(502,e.getMessage(),null);
        }
    }
}
